package com.sundaying.gooway.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.sundaying.gooway.dao.TopItemDao;
import com.sundaying.gooway.entity.TopClass;
import com.sundaying.gooway.entity.TopItem;
import com.sundaying.gooway.entity.UrlClass;
import com.sundaying.gooway.entity.UrlItem;

@Component("topItemDao")
public class TopItemDaoImpl implements TopItemDao {
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Resource(name = "sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public int save(TopItem obj) {
		int result=0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.save(obj);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
		result = 1;
		return result;
	}

	@Override
	public int update(TopItem obj) {
		int result=0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.update(obj);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
		result = 1;
		return result;
	}

	@Override
	public int delete(TopItem obj) {
		int result=0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		session.delete(obj);
		session.getTransaction().commit();
		session.close();
		sessionFactory.close();
		result = 1;
		return result;
	}

	@Override
	public TopItem load(long id) {
		TopItem topItem = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		topItem = (TopItem) session.load(TopItem.class, id);
		session.getTransaction().commit();
		session.close();
		return topItem;
	}

	@Override
	public List<TopItem> list() {
		List<TopItem> topItemList = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery("from TopItem");
		topItemList = (List<TopItem>) query.list();
		session.getTransaction().commit();
		session.close();
		return topItemList;
	}

	@Override
	public List<TopItem> list(String hsql, List<Object> param) {
		List<TopItem> topItemList = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery(hsql);
		if (param != null && param.size() > 0) {
			int size = param.size();
			for (int i = 0; i < size; i++) {
				query.setParameter(i, param.get(i));
			}
		}

		topItemList = (List<TopItem>) query.list();
		session.getTransaction().commit();
		session.close();
		return topItemList;
	}

	@Override
	public List<TopItem> list(String hsql, List<Object> param, int page,
			int rows) {
		List<TopItem> topItemList = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery(hsql);
		if (param != null && param.size() > 0) {
			int size = param.size();
			for (int i = 0; i < size; i++) {
				query.setParameter(i, param.get(i));
			}
		}
		// 第一条记录的序号为0
		query.setFirstResult((page - 1) * rows);;  
		query.setMaxResults(rows);;  
		topItemList = (List<TopItem>) query.list();
		session.getTransaction().commit();
		session.close();
		return topItemList;
	}

	@Override
	public long count(String hql, List<Object> param) {
		long result = 0;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		if (param != null && param.size() > 0) {
			int size = param.size();
			for (int i = 0; i < size; i++) {
				query.setParameter(i, param.get(i));
			}
		}
		result = (Long) query.uniqueResult();
		session.getTransaction().commit();
		session.close();
		return result;
	}

	@Override
	public List<UrlItem> getUrlItemList(String hql, List<Object> param, int rows) {
		List<UrlItem> topItemList = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		if (param != null && param.size() > 0) {
			int size = param.size();
			for (int i = 0; i < size; i++) {
				query.setParameter(i, param.get(i));
			}
		}
		query.setFirstResult(0);
		query.setMaxResults(rows);
		topItemList = (List<UrlItem>) query.list();
		session.getTransaction().commit();
		session.close();
		return topItemList;
	}

	@Override
	public List<UrlClass> getUrlClassList(String hql, List<Object> param,
			int rows) {
		List<UrlClass> topItemList = null;
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query query = session.createQuery(hql);
		if (param != null && param.size() > 0) {
			int size = param.size();
			for (int i = 0; i < size; i++) {
				query.setParameter(i, param.get(i));
			}
		}
		query.setFirstResult(0);
		query.setMaxResults(rows);
		topItemList = (List<UrlClass>) query.list();
		session.getTransaction().commit();
		session.close();
		return topItemList;
	}

}
